package com.app.conrtoller;

import com.app.domain.ExternalResponse;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.hsy.SendMessage;
import com.app.domain.lhgs.*;
import com.app.service.SendMessageService;
import com.app.service.UploadParameterService;
import com.app.service.UploadService;
import com.app.service.YjDataService;
import com.app.utils.TimeHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/lhgs")
public class LhgsUploadController {
    @Value("${yj.lhgs.serviceHost}")
    private String serviceHost;
    @Value("${yj.lhgs.manufacturerId}")
    private String manufacturerId;
    @Value("${yj.lhgs.equipmentCode}")
    private String equipmentCode;
    @Value("${yj.lhgs.code}")
    private String code;
    @Value("${yj.lhgs.equipmentId}")
    private String equipmentId;
    @Value("${yj.lhgs.yjEquipmentId}")
    private String yjEquipmentId;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    UploadService uploadService;
    @Autowired
    SendMessageService sendMessageService;

    @RequestMapping("/upload")
    @ResponseBody
    public ExternalResponse upload(@RequestParam(value = "ids[]") String[] ids) throws InterruptedException {
        ExternalResponse externalResponse = null;
        int total = ids.length;
        int succTotal = 0;
        int failTotal = 0;
        StringBuffer responseContent = new StringBuffer();
        responseContent.append("本次共上传数据：【" + total + "】条,");
//        String uuid = sendMessageService.ConditionQuery(ids[0]);
        //获取生产厂家信息数据
        List<Manufacturer> manufacturerList = getManufacturer();
        List<Equipment> yjEquipment = getEquipment();
//        UploadParameter uploadParameter = uploadParameterService.findOne("8");
        List<YjData> yjDataList = new ArrayList<>();
        for (String id : ids) {

            YjData yjData = yjDataService.findOne(id);
            yjDataList.add(yjData);
        }
        List<YJBeamData> yjBeamDataList = uploadService.getLhYjData(yjDataList, equipmentId);
        JsonData jsonData = new JsonData();
        jsonData.setManufacturer(manufacturerList);
        jsonData.setEquipment(yjEquipment);
        jsonData.setYjBeamData(yjBeamDataList);
        ResultJsonData resultJsonData = new ResultJsonData();
        resultJsonData.setLicense(System.currentTimeMillis());
        resultJsonData.setCode(code);
        resultJsonData.setUploadType(4);
        resultJsonData.setEquipmentId(yjEquipmentId);
        resultJsonData.setData(jsonData);
        LhResponseResult hnResponseResult =uploadService.sendLhMessage(serviceHost, resultJsonData);
        if (hnResponseResult.getResult() == 100) {
            /*if (StringUtils.isEmpty(uuid)){
                //主表上传成功
                SendMessage sendMessage = new SendMessage();
                sendMessage.setSbbh(yjDataList.get(0).getDevice_code());
                sendMessage.setLh(yjDataList.get(0).getBridge_code());
                sendMessage.setKh("N1");
                sendMessage.setBusi_type("SWYJSY_M");
                sendMessage.setUpload_time(new Date());
                sendMessage.setUuid(yjBeamDataList.get(0).getId());
                sendMessage.setStatus("0");
                sendMessageService.insert(sendMessage);
            }*/
            succTotal = ids.length;
            for (YjData yjData : yjDataList) {
                updateState("1", String.valueOf(yjData.getId()));
            }
            log.info("-------------压浆主记录数据发送成功------------------------");
        } else {
            for (YjData yjData : yjDataList) {
                updateState("0", String.valueOf(yjData.getId()));
            }
            failTotal = ids.length;
            log.info("-------------压浆主记录数据发送失败------------------------");
        }
        responseContent.append("成功【" + succTotal + "】条,失败" + failTotal + "条");
        externalResponse = new ExternalResponse(0, responseContent.toString(), "");
        return externalResponse;
    }


    private void updateState(String state, String id) {
        yjDataService.updateById(state, id);
    }

    public List<Manufacturer> getManufacturer() {
        List<Manufacturer> manufacturers = new ArrayList<>();
        Manufacturer manufacturer = new Manufacturer();
        manufacturer.setId(manufacturerId);
        manufacturer.setName("开封齐力预应力有限公司");
        manufacturer.setUnifiedSocialCreditCode("91610131583199495N");
        manufacturer.setRemark("/");
        manufacturer.setCreatedTime(TimeHelper.getCurrentTime());
        manufacturers.add(manufacturer);
        return manufacturers;
    }

    public List<Equipment> getEquipment() {
        List<Equipment> equipmentList = new ArrayList<>();
        Equipment equipment = new Equipment();
        equipment.setId(equipmentId);
        equipment.setName("压浆设备01");
        equipment.setType(2);
        equipment.setManufacturerId(manufacturerId);
        equipment.setDateOfProduction("2022-03-11 16:29:53");
        equipment.setRemark("/");
        equipment.setCreatedTime(TimeHelper.getCurrentTime());
        equipment.setCode(equipmentCode);
        equipment.setServiceFacilityID(yjEquipmentId);
        equipmentList.add(equipment);
        return equipmentList;
    }
}
